package com.my.algorithmlearning.leetcode.slidewindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName Hard_30_SubstringWithConcatenationOfAllWords
 * @Description 题目：<a href="https://leetcode.cn/problems/substring-with-concatenation-of-all-words/description/?envType=study-plan-v2&envId=top-interview-150">...</a>
 * {@code @Author} wanghaiwei
 */
public class Hard_30_SubstringWithConcatenationOfAllWords {
    /*    public List<Integer> findSubstring(String s, String[] words) {
            int sLength = s.length();
            int wNum = words.length;
            int wLen = words[0].length();
            if (sLength < wNum * wLen) {
                return new ArrayList<>();
            }
            //构造单词哈希表
            Map<String, Integer> orgWordMap = new HashMap<>();
            for(String w : words) {
                orgWordMap.putIfAbsent(w, 1);
                orgWordMap.computeIfPresent(w, (k,v) -> v + 1);
            }
            int i = 0;
            //构造滑动的边界
            while(i < (sLength - (wNum * wLen))) {
                int maxIdxOfAbsentStr = -1;
                Map<String, Integer> currSlideWindowWordMap = new HashMap<>();
                for(int j = i; j < (i + (wNum * wLen)); j = j + wLen) {

                }
            }

        }*/
    public List<Integer> findSubstring(String s, String[] words) {
        int n = s.length(), m = words.length, w = words[0].length();
        Map<String, Integer> map = new HashMap<>();
        for (String word : words) map.put(word, map.getOrDefault(word, 0) + 1);
        List<Integer> ans = new ArrayList<>();
        out:
        for (int i = 0; i + m * w <= n; i++) {
            Map<String, Integer> cur = new HashMap<>();
            String sub = s.substring(i, i + m * w);
            for (int j = 0; j < sub.length(); j += w) {
                String item = sub.substring(j, j + w);
                if (!map.containsKey(item)) continue out;
                cur.put(item, cur.getOrDefault(item, 0) + 1);
            }
            if (cur.equals(map)) ans.add(i);
        }
        return ans;
    }
}
